//统一管理咱们项目用户相关的接口
// import request from '@/utils/http'
import { loginHttpRequest as request } from '@/utils/http';
import defaultHttpRequest from '@/utils/http';
import type {
  LoginFormRequest,
  LoginFormResponse,
  IdentityUserDto,
  UserCreateDto,
  UserUpdateDto,
  GetIdentityUsersInput,
  IdentityUserDetailDto,
  IdentityUserUpdateRolesDto,
  UserAvatarUpdateDto,
  SearchUserListInputDto,
  SearchUserOutputDto
} from './type';
import { getApiUrl, jsonToUrlParam } from '@/utils/urlSearchParams';
import type { ListResultDto, PagedResultDto } from '../type';
import type { IdentityRoleDto, RoleDto } from '../role/type';

enum API {
  LOGIN_URL = '/api/basics/accounts/login',
  USERINFO_URL = 'api/basics/users',
  LOGOUT_URL = '/admin/acl/index/logout'
}

//登录接口
export const reqLogin = (data: LoginFormRequest) =>
  request.post<LoginFormResponse>(API.LOGIN_URL, data);

//获取用户信息
export const reqUserInfo = (id: string) =>
  defaultHttpRequest.get<IdentityUserDto>(`${API.USERINFO_URL}/${id}`);

//退出登录
// export const reqLogout = () => request.post<any, any>(API.LOGOUT_URL)

//列表查询
export const getListAsync = (data: GetIdentityUsersInput) => {
  const paramenter = jsonToUrlParam(data);
  const url = getApiUrl(API.USERINFO_URL, paramenter);
  return defaultHttpRequest.get<PagedResultDto<IdentityUserDto>>(url);
};

//创建用户
export const createAsync = (data: UserCreateDto) =>
  defaultHttpRequest.post<IdentityUserDto>(API.USERINFO_URL, data);

//修改用户
export const updateAsync = (id: string, data: UserUpdateDto) =>
  defaultHttpRequest.put<IdentityUserDto>(`${API.USERINFO_URL}/${id}`, data);

//删除用户
export const deleteAsync = (id: string) =>
  defaultHttpRequest.delete<void>(`${API.USERINFO_URL}/${id}`);

//获取用户角色
export const getUserRolesAsync = (id: string) =>
  defaultHttpRequest.get<ListResultDto<IdentityRoleDto>>(`${API.USERINFO_URL}/${id}/roles`);

//获取用户信息
export const getAsync = (id: string) =>
  defaultHttpRequest.get<IdentityUserDetailDto>(`${API.USERINFO_URL}/${id}/detail`);

//用户角色设置
export const setUserRolesAsync = (id: string, data: IdentityUserUpdateRolesDto) =>
  defaultHttpRequest.put<void>(`${API.USERINFO_URL}/${id}/roles`, data);

//更新用户头像
export const updateAvatarAsync = (id: string, data: UserAvatarUpdateDto) =>
  defaultHttpRequest.put<void>(`${API.USERINFO_URL}/${id}/avatar`, data);

// 用户选择
export const getUsersAsync = (data: SearchUserListInputDto) => {
  const paramenter = jsonToUrlParam(data);
  const url = getApiUrl(`${API.USERINFO_URL}/user-select`, paramenter);
  return defaultHttpRequest.get<Array<SearchUserOutputDto>>(url);
};

// 角色选择
export const getRoleAsync = () => {
  return defaultHttpRequest.get<Array<RoleDto>>(`${API.USERINFO_URL}/role-select`);
};
